﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Agency_TraCheckIsbidcompany_PSM : System.Web.UI.Page
{
    public string _strProjectId;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupplyCompanyCheck);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            _strProjectId = SysFun.IsSQL(Request.QueryString["ProjectId"].ToString());
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (!IsPostBack)
            {
             
                if (_strProjectId == "9")
                {
                    Button1.Visible = true; 
                    if (_Account.Type == "1") Button3.Visible = true;
                    if (_Account.Type == "1") Button4.Visible = true;
                }
            }
            showGridView();
        }
    }
    public void showGridView()
    {
        int i = 0;
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods";                                               //要查询的表明
        dbPage.Key = "GoodsId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "SupplyGoodsId,GoodsId,ProductName,MedicineModel,Outlookc,Factor,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,UserCode_TB,CompanyName_TB,UserCode_PS,CompanyName_PS,case PS_ChangeStatus when '0' then '未审核' when '1' then '审核通过' when '2' then '审核未通过' end as PS_ChangeStatus,Accountname_shr,NopssReason ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 20;                                                    //没有显示记录条数   
        dbPage.OrderBy = "CompanyName_TB,productname,medicinemodel";//排序字段


        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='center' cellpadding='2' cellspacing='1' class='table_table'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td class='table_title' nowrap>流水号</td>");
        strHtml.Append("<td class='table_title' nowrap>通用名</td>");
        strHtml.Append("<td class='table_title' nowrap>剂型</td>");
        strHtml.Append("<td class='table_title' nowrap>规格</td>");
        strHtml.Append("<td class='table_title' nowrap>转换系数</td>");
        strHtml.Append("<td class='table_title' nowrap>质量层次</td>");
        strHtml.Append("<td class='table_title' nowrap>中标人注册号</td>");
        strHtml.Append("<td class='table_title' nowrap>中标人名称</td>");
        strHtml.Append("<td class='table_title' nowrap>配送企业名称</td>");
        strHtml.Append("<td class='table_title' nowrap>审核状态</td>");
        strHtml.Append("<td class='table_title' nowrap>审核人</td>");
        strHtml.Append("<td class='table_title' nowrap>审核</td>");
        if (_strProjectId == "9")
        {
            strHtml.Append("<td class='table_title' nowrap>删除</td>");
        }
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();

        while (dr.Read())
        {
            string _strCheckStatus = dr["PS_ChangeStatus"].ToString();
            if (_strCheckStatus == "2")
            {
                strHtml.Append("<tr style='background-color:Yellow'>");
            }
            else
            {
                if (i == 0)
                {
                    strHtml.Append("<tr class='table_td'>");
                    i++;
                }
                else
                {
                    strHtml.Append("<tr class='table_td2'>");
                    i--;
                }
            }

            strHtml.Append("<td>" + dr["goodsid"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["ProductName"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["MedicineModel"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Outlookc"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["Factor"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["QualityLevels"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["UserCode_TB"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_TB"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["CompanyName_PS"].ToString() + "</td>");
           
            if (dr["PS_ChangeStatus"].ToString() == "审核未通过")
            {
                strHtml.Append("<td title='" + dr["NopssReason"].ToString() + "' style=' color: #ff0000;' >" + dr["PS_ChangeStatus"].ToString() + "</td>");
            }
            else
            {
                strHtml.Append("<td >" + dr["PS_ChangeStatus"].ToString() + "</td>");
            }
            strHtml.Append("<td>" + dr["Accountname_shr"].ToString() + "</td>");//审核人
            if (dr["CompanyName_PS"].ToString().Trim() == "")
            {
                strHtml.Append("<td>企业未维护</td>");
            }
            else
            {
                //strHtml.Append("<td><a target='_blank' href=\"TraCheckIsbidcompany_PSMinfo.aspx?GoodsId='"+dr["goodsid"].ToString()+"'\" onClick=\"window.open(this.href,'+''''+''''+','+''''+'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=1'+ ''''+');return false;\">审核</a></td>");
                strHtml.Append("<td><a target='_blank' href=\"TraCheckIsbidcompany_PSMinfo.aspx?GoodsId=" + dr["goodsid"].ToString() + "\" onClick=\"javascript:window.open(this.href,'','menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=1');return false;\">审核</a></td>");
            }
            if (_strProjectId == "9")
            {
                strHtml.Append("<td ><input name='chk' type='checkbox' value='" + dr["SupplyGoodsId"].ToString() + "' /></td>");
            }
            strHtml.Append("</tr>");
            
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();
        //BId = Request.Form["bidid"].ToString();
        //GridView1.DataSource = dr;
        //GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }

    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp, CheckStatus,_CheckStatus_PS, _ps, _GoodsId, name, outlookc,_companyname_tb,_usecoder_tb;
       
        name = ProductName.Text.ToString();
        outlookc = Outlookc.Text.ToString();

        _companyname_tb = Textcompanyname_tb.Text.Trim();
        _usecoder_tb = txtCompanyUserCode.Text.Trim();
       // strtemp = " projectid=" + _strProjectId + " and UserCode_TB like 's%' and isuse=1 and";
        strtemp = " isuse=1 and";
        _ps = SysFun.ToTrim(txtps.Text);
        _GoodsId = SysFun.ToTrim(txtGoodsId.Text);
     
        //审核状态
        CheckStatus = lstCheckStatus.SelectedValue;
        switch (CheckStatus)
        {
            case "3":  //全部
                break;
            case "0"://未审核
                strtemp += " PS_ChangeStatus=0 and";
                break;
            case "1"://审核通过
                strtemp += " PS_ChangeStatus=1 and";
                break;
            case "2"://审核未通过
                strtemp += " PS_ChangeStatus=2 and";
                break;
        }
       //提交状态
        _CheckStatus_PS = DropDownList1.SelectedValue;
        switch (_CheckStatus_PS)
        {
            case "2"://未维护
                strtemp += " (CompanyName_PS is null or CompanyName_PS='')and";
                break;
            case "1"://已维护
                strtemp += " (CompanyName_PS is not null or CompanyName_PS<>'')  and";
                break;
            case "0"://全部 
                break;
        }
        if (_ps != "") //按配送企业查询
        {
            strtemp += " CompanyName_PS like '%" + _ps + "%' and";
        }


        if (_GoodsId != "")
        {
            strtemp += " GoodsId like '%" + _GoodsId + "%' and";

        }
    
        
        if (name != "")
        {
            strtemp += " (ProductName like '%" + name + "%'or Product_SpellingCode like '%" + name + "%') and";
        }
        if (outlookc != "")
        {
            strtemp += " outlookc like '%" + outlookc + "%' and";
        }

        if (_companyname_tb != "")
        {
            strtemp += " CompanyName_TB like '%" + _companyname_tb + "%' and";
        }

        if (_usecoder_tb != "")
        {
            strtemp += " UserCode_TB like '%" + _usecoder_tb + "%' and";
        }

        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }

        return strtemp;

    }


    protected void ButPrint_Click(object sender, EventArgs e)
    {
        if ((Textcompanyname_tb.Text.Trim() == "") && (txtCompanyUserCode.Text.Trim()==""))
        {
            Response.Write("<script>alert('请填写中标人')</script>");
        }
        else
        {
            if (Textcompanyname_tb.Text.Trim() == "")
            {
                string set="select companyname from company where usercode='"+txtCompanyUserCode.Text.Trim()+"'";
                Db d = new Db();
                DataTable dt = d.DataTable(set);
                d.Close();
                if (dt.Rows.Count > 0)
                {
                    Response.Write("<script>open('TraBidPrint.aspx?companyname_tb=" + dt.Rows[0][0].ToString() + "')</script>");
                }
            }
            else
            {
                Response.Write("<script>open('TraBidPrint.aspx?companyname_tb=" + Textcompanyname_tb.Text.Trim() + "')</script>");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)//删除
    {
        string _accountid = "0";
        if ((Textcompanyname_tb.Text.Trim() == "") && (txtCompanyUserCode.Text.Trim() == ""))
        {
            Response.Write("<script>alert('请填写中标人')</script>");
        }
        else
        {
            string set = "select companyname,accountid from company where usercode='" + txtCompanyUserCode.Text.Trim() + "' or companyname='" + Textcompanyname_tb.Text.Trim() + "'";
            Db d = new Db();
            DataTable dt = d.DataTable(set);
            d.Close();
            if (dt.Rows.Count > 0)
            {
                _accountid = dt.Rows[0][1].ToString();
            }

            if (Request.Form["chk"] != null)
            {
                String StrSql = "", strid = "";

                strid = Request.Form["chk"];
                string[] SGID = strid.Length == 0 ? new string[] { "" } : strid.Substring(0).Split(new char[] { ',' });
                StrSql = "";

                for (int i = 0; i < SGID.Length; i++)
                {
                    StrSql += "@delete SupplyPlan_Goodsnext where SupplyGoodsId=" + SGID[i].ToString();

                    StrSql += "@delete HospitalPurchase where SupplyGoodsId=" + SGID[i].ToString();
                    StrSql += "@delete project_bid where convert(varchar(200),GOODSID)+convert(varchar(200),accountid) in(SELECT convert(varchar(200),goodsid)+convert(varchar(200)," + _accountid + ") from SupplyPlan_Goods where SupplyGoodsId=" + SGID[i].ToString()+")";
                    StrSql += "@delete SupplyPlan_Goods where SupplyGoodsId=" + SGID[i].ToString();
                }
                if (StrSql.Length > 0)
                {
                    Db db = new Db();
                    db.ExecuteTransaction(StrSql.Substring(1).Split(new char[] { '@' }), 30);
                    db.Close();
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "SupplyPlan_GoodsNext,HospitalPurchase", StrSql, "../Agency/TraCheckIsbidcompany_PSF.aspx");

                }
                showGridView();
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)  //重点监控
    {
        if ((Textcompanyname_tb.Text.Trim() == "") && (txtCompanyUserCode.Text.Trim() == ""))
        {
            Response.Write("<script>alert('请填写中标人')</script>");
        }
        else
        {
            if (Textcompanyname_tb.Text.Trim() == "")
            {
                string set = "select companyname from company where usercode='" + txtCompanyUserCode.Text.Trim() + "'";
                Db d = new Db();
                DataTable dt = d.DataTable(set);
                d.Close();
                if (dt.Rows.Count > 0)
                {
                    Response.Write("<script>open('TraBidPrint.aspx?companyname_tb=" + dt.Rows[0][0].ToString() + "&id=j')</script>");
                }
            }
            else
            {
                Response.Write("<script>open('TraBidPrint.aspx?companyname_tb=" + Textcompanyname_tb.Text.Trim() + "&id=j')</script>");
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.open('excel.aspx');</script>");
    }
    protected void Button4_Click(object sender, EventArgs e) 
    {
        Response.Write("<script>window.open('company_isprint.aspx');</script>");
    }
}
